#ifndef THREADPOOL_H
#define THREADPOOL_H
#include "thread.h"
#include <map>
#include <set>
#include <atomic>

enum ThreadPoolStatus
{
    POOL_STATUS_UNINIT,
    POOL_STATUS_INITING,
    POOL_STATUS_INITED,
    POOL_STATUS_RUNING,
    POOL_STATUS_RUN,
    POOL_STATUS_STOPPING,
    POOL_STATUS_STOPPED
};
class ThreadPool : public std::enable_shared_from_this<ThreadPool>
{
private:
    std::atomic_int status_;
    const int threadnum_;
    std::map<int, std::shared_ptr<Thread>> threads_;
    std::map<int, std::weak_ptr<Thread>> freethreadset_;
    std::map<int, std::weak_ptr<Thread>> workthreadset_;
    std::mutex threadmutex_;
    int MoveThread2Free(int id);
    int MoveThread2Work(int id);
    int lasteq;

public:
    ThreadPool(int nums);
    int Init();
    int Fini();
    ~ThreadPool();
    int PostTask(FuncType func, std::shared_ptr<void> args);
    int Notify(int id, int cmd);
};
#endif
